<style src="./index.scss" lang="scss" scoped rel="stylesheet/scss"></style>
<template src="./index.html"></template>
<script>
    import Edit from "./edit/index";
    export default {
        mixins: [Vue.prototype.$vFN.Mixins.Base],
        components: {
            Edit
        },
        props: {
            form: {
                type: Object,
                default: null,
            },
            menuLst: {
                type: Array,
                default: [],
            },
            appWindow: {
                type: Object,
                default: null,
            },

        },
        computed: {

        },

        data() {

            return {
                selectItemIndex: 0,
                selectSubItemIndex: -1,

                itemMode: {
                    // 菜单名称
                    name: "新建菜单",
                    // 菜单的响应动作类型 view网页类型 miniprogram 小程序类型 click发送消息
                    type: "click",

                    // 内容类型 文本text 图片 image 语音 voice 视频 video 图文 news
                    msgType: "text",
                    // 回复的消息内容  msgType=text时传
                    content: "",
                    // 资源 msgType=image、voice、video、news 时传
                    albumInfoId: "",
                    // 小程序APPID
                    appid: "",
                    // 小程序页面路径
                    pagepath: "",
                    // 备用网页url / 跳转链接
                    url: "",

                    // 内容
                    _content: {
                        text: "",
                        image: null,
                        voice: null,
                        video: null,
                        news: null,
                        keyword: null,
                    },

                },
            };
        },
        methods: {

            getFormData() {
                let _form = JSON.parse(JSON.stringify(this.form));
                let _menuLst = JSON.parse(JSON.stringify(this.menuLst));

                let _list = []

                _menuLst.forEach((item, index) => {

                    let _d = this.getFormItemData(item);

                    if (!this.$vFN.Utils.isEmpty(item.subMenuLst)) {
                        _d.subMenuLst = []
                        item.subMenuLst.forEach((iitem, index) => {
                            _d.subMenuLst.push(this.getFormItemData(iitem))
                        })
                    }
                    _list.push(_d)
                })
                return _list
            },
            getFormItemData(data_) {
                let _d = {
                    albumInfoId: "",
                    appid: data_.appid,
                    content: "",
                    msgType: data_.msgType,
                    name: data_.name,
                    pagepath: data_.pagepath,
                    type: data_.type,
                    url: data_.url,
                }

                if (data_.msgType == "text") {
                    _d.content = data_._content.text
                } else if (data_.msgType == "image") {
                    _d.albumInfoId = data_._content.image.id
                } else if (data_.msgType == "video") {
                    _d.albumInfoId = data_._content.video.id
                } else if (data_.msgType == "voice") {
                    _d.albumInfoId = data_._content.voice.id
                } else if (data_.msgType == "news") {
                    _d.albumInfoId = data_._content.news.id
                }else if (data_.msgType == "keyword") {
                    _d.replyId = data_._content.keyword.id
                }
                return _d
            },
            delItem(index_, iindex_) {
                console.error(index_, iindex_)
                this.$confirm("确定删除此配置么", {
                        confirmButtonText: "确定",
                        cancelButtonText: "取消",
                        dangerouslyUseHTMLString: true,
                    }).then(async () => {
                        if (iindex_ > -1) {
                            this.menuLst[index_].subMenuLst.splice(iindex_, 1);
                        } else {
                            this.menuLst.splice(index_, 1);
                        }
                        this.selectItemIndex = 0;
                        this.selectSubItemIndex = -1;
                    })
                    .catch(() => {});
            },
            // 设置表单数据
            toForm() {
                let _index = this.selectItemIndex;
                let _iindex = this.selectSubItemIndex;
                let _item = null;
                if (_iindex > -1) {
                    _item = this.menuLst[_index].subMenuLst[_iindex]
                } else {
                    _item = this.menuLst[_index]
                }
                for (let i in this.itemMode) {
                    this.form[i] = _item[i]
                }
            },
            seletItem(index_, iindex_) {
                this.selectItemIndex = index_;
                this.selectSubItemIndex = iindex_;
                // this.toForm();
            },
            // 添加菜单子项
            addMenuItem() {
                let _d = JSON.parse(JSON.stringify(this.itemMode))
                _d.subMenuLst = []
                this.menuLst.push(_d)
                if(this.selectItemIndex>0){
                    this.selectItemIndex = this.selectItemIndex+1;
                }

            },
            // 添加菜单子项
            addMenuLi(index_) {
                let _d = JSON.parse(JSON.stringify(this.itemMode))
                _d.name = "子菜单"
                this.menuLst[index_].subMenuLst.unshift(_d)
                this.selectItemIndex = index_;
                this.selectSubItemIndex = 0;
            },
        },
        mounted() {},
        beforeCreate() {},
        watch: {
            // "form": {
            //     handler(newName, oldName) {
            //         this.returnData();
            //     },
            //     immediate: true,
            //     deep: true,
            // },
        },
    };
</script>
